home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / msgscan.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  11KB  |  384 lines

  1. /*  Citadel Message Base Scanner */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "ctdl.h"
  5.  
  6. typedef enum { NO=0, YES=1 }  BOOLEAN_TYPE;
  7.  
  8. char netDebug      = '\0';
  9. char logNetResults = '\0';
  10. FILE  *netLog = stdout;
  11. char verbose_flag  = FALSE;
  12.  
  13. extern CONFIG      cfg;            /* A buncha variables           */
  14. extern FILE            *netfl, *roomfl;
  15. extern MessageBuffer  msgBuf; /* The -sole- message buffer      */
  16. extern FILE *msgfl, *msgfl2;
  17.  
  18. void Print_Items(struct headlist *ptr);
  19. void Match_Message(MessageBuffer *msg);
  20. void Do_Statistics(MessageBuffer *msg);
  21. void Print_Statistics(void);
  22. void Increment( struct headlist **cptr, char *name);
  23. void Scan_Messages(void);
  24. void crashout(char *);
  25. int main(int argc, char **argv);
  26. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  27. void A_Menu(void);
  28. void Terminal(char *string,int  size);
  29. void strip_crlf(char *string);
  30. BOOLEAN_TYPE match(char *str1, int idx);
  31.  
  32. void crashout(str)
  33. char *str;
  34.   {
  35.   printf(str);
  36.   exit(10);
  37.  
  38.   }
  39.  
  40. int  main(argc,argv)
  41. int  argc;
  42. char **argv;
  43.   {
  44.   /* Process CONFIG  */
  45.   cfg.weAre = UTILITY;
  46.   printf("Citadel Message Base Scanner Version %s\n", VERSION_NAME);
  47.   if (!readSysTab(FALSE, TRUE)) exit(100);
  48.   A_Menu();
  49.   Scan_Messages();    /* process the messages */
  50.   Print_Statistics();
  51.   return 0;
  52.   }
  53.  
  54. void Scan_Messages()
  55.   {
  56.   MSG_NUMBER msg, firstMessage;
  57.   int total=1;     /* For stat keeping. */
  58.   extern struct mBuf mFile1;
  59.   fprintf(stderr, "Mulching...\n");
  60.   InitMsgBase();
  61.   startAt(msgfl, &mFile1, 0, 0);
  62.   getMessage(getMsgChar, FALSE, TRUE, TRUE);
  63.   firstMessage = atol(msgBuf.mbId);
  64.   msg = firstMessage -1;
  65.   while (msg != firstMessage)
  66.     {
  67.     total++;
  68.     getMessage(getMsgChar, FALSE, TRUE, TRUE);
  69.     msg = atol(msgBuf.mbId);
  70.     Match_Message(&msgBuf);
  71.     };
  72.   }
  73.  
  74.  
  75. #define MAX_OPTIONS  (12)
  76. #define MAX_FIELD    (150)
  77.  
  78. struct select
  79.   {
  80.   BOOLEAN_TYPE  option_enabled;  /* Is this option enabled */
  81.   char          option_name[20]; /* name of the option     */
  82.   int           size;            /* size of object */
  83.   };
  84.  
  85. struct select options[MAX_OPTIONS] =
  86.   {
  87.   NO, "Author             ", 129,             NO, "Creation Date      ", sizeof(label),
  88.   NO, "Creation Time      ", sizeof(label),   NO, "Id Number          ", sizeof(label),
  89.   NO, "Node Name          ", sizeof(label),   NO, "Origin Name        ", sizeof(label),
  90.   NO, "Room Name          ", sizeof(label),   NO, "To User            ", 129,
  91.   NO, "Net Routing Address", (NAMESIZE*2)+10, NO, "OtherNet Address   ", O_NET_PATH_SIZE,
  92.   NO, "Reply              ", sizeof(label),   NO, "Domain             ", sizeof(label),
  93.   };
  94.  
  95. char *option_data[MAX_OPTIONS];
  96.  
  97. void A_Menu()
  98.   {
  99.   int idx;
  100.   char ans;
  101.   char line[MAX_FIELD];
  102.   ans = ' ';
  103.   for(idx=0; idx < MAX_OPTIONS; idx++)
  104.     {
  105.     options[idx].option_enabled = NO;
  106.     option_data[idx] = NULL;
  107.     };
  108.   while( ans != 'Q' && ans != 'q'
  109.       && ans != 'X' && ans != 'x')
  110.     {
  111.     printf("  Message Selection Options\n");
  112.     for( idx=0; idx < MAX_OPTIONS; idx++ )
  113.       {
  114.       if( ( idx % 2 ) == 0 )printf("\n");
  115.       printf( " %c - %20s(%8s)",( 'A'+idx ), options[idx].option_name,
  116.       ( options[idx].option_enabled == YES ? "enabled" : "disabled"));
  117.       };
  118.     printf("\n Verbose is %s", (verbose_flag== YES) ? "on." : "off.");
  119.     printf("\n Q - to quit,  X - to exit and do scan:");
  120.     Terminal(line,1);
  121.     ans = line[0];
  122.     ans = toupper(ans);
  123.     idx = ans - 'A';
  124.     if( ans == 'V' || ans == 'v')
  125.       {
  126.       verbose_flag = ( verbose_flag == YES ) ? NO : YES ;
  127.       };
  128.     if( ans != 'Q' && ans != 'q'
  129.      && ans != 'x' && ans != 'X' )
  130.       {
  131.       if( idx < 0 || idx >= MAX_OPTIONS )
  132.         {
  133.         ans = ' ';
  134.         printf("Error: Must enter A thru L\n");
  135.         }
  136.       else
  137.         {
  138.         options[idx].option_enabled =  options[idx].option_enabled == YES ? NO : YES;
  139.         if( options[idx].option_enabled == YES )
  140.           {
  141.           printf("Enter %s(up to %d long):",options[idx].option_name, options[idx].size);
  142.           Terminal(line,options[idx].size);
  143.           option_data[idx] = strdup(line);
  144.           }
  145.         else
  146.           {
  147.           free(option_data[idx]);
  148.           }
  149.         };
  150.       };
  151.     };
  152.   if( ans == 'q' || ans == 'Q' ) exit(0);
  153.   }
  154.  
  155. void strip_crlf(char *string)
  156.   {
  157.   int x;
  158.   for(x =  strlen(string); x>=0; x--)
  159.     {
  160.     if((string[x] ==  '\r')||(string[x] ==  '\n')) string[x] =  0;
  161.  
  162.     }
  163.  
  164.   }
  165.  
  166. void Terminal(char *string,int  size)
  167.   {
  168.   char  c;
  169.   short i;
  170.   if( size > 8 )
  171.     {
  172.     for( i = 0; i < size; ) string[i++] = '\0';
  173.     for (i = 0; i < size && ( c = fgetchar() ) != '\n' && c != EOF;i++)
  174.       {
  175.       string[i] = c;
  176.       };
  177.     while ( c != '\n' && c != EOF  && c != 0x03 )c = fgetchar();
  178.     }
  179.   else
  180.     {
  181.     char tmp[8];
  182.     for (i = 0; i < 8 && ( c = fgetchar() ) != '\n' && c != EOF;i++)
  183.       {
  184.       tmp[i] = c;
  185.       };
  186.     while ( c != '\n' && c != EOF && c != 0x03 )c = fgetchar();
  187.     strncpy(string,tmp,size);
  188.     };
  189.   strip_crlf(string);
  190.   if( string[0] == '\0' )
  191.     {
  192.     string[0] = '\n';
  193.     string[1] = '\0';
  194.     };
  195.   }
  196.  
  197. void Match_Message(MessageBuffer *msg)
  198.   {
  199.   int idx;
  200.   BOOLEAN_TYPE a_match;
  201.  
  202.   for( a_match = YES, idx=0; idx < MAX_OPTIONS && a_match == YES; idx++ )
  203.     {
  204.     if( options[idx].option_enabled == YES)
  205.       {
  206.       switch (idx)
  207.         {
  208.         case  0: if( match(msg->mbauth,  idx) == NO) a_match = NO; break;
  209.         case  1: if( match(msg->mbdate,  idx) == NO) a_match = NO; break;
  210.         case  2: if( match(msg->mbtime,  idx) == NO) a_match = NO; break;
  211.         case  3: if( match(msg->mbId,    idx) == NO) a_match = NO; break;
  212.         case  4: if( match(msg->mboname, idx) == NO) a_match = NO; break;
  213.         case  5: if( match(msg->mborig,  idx) == NO) a_match = NO; break;
  214.         case  6: if( match(msg->mbroom,  idx) == NO) a_match = NO; break;
  215.         case  7: if( match(msg->mbsrcId, idx) == NO) a_match = NO; break;
  216.         case  8: if( match(msg->mbaddr,  idx) == NO) a_match = NO; break;
  217.         case  9: if( match(msg->mbOther, idx) == NO) a_match = NO; break;
  218.         case 10: if( match(msg->mbreply, idx) == NO) a_match = NO; break;
  219.         case 11: if( match(msg->mbdomain,idx) == NO) a_match = NO; break;
  220.         };
  221.       };
  222.     };
  223.   if( a_match == YES )
  224.     {
  225.     if( verbose_flag )
  226.       {
  227.       printf( "Author: %20s  ",msg->mbauth  );
  228.       printf( "Node Id:%20s\n",msg->mbId    );
  229.       printf( "Date:   %20s  ",msg->mbdate  );
  230.       printf( "Time:   %20s\n",msg->mbtime  );
  231.       printf( "Node:   %20s  ",msg->mboname );
  232.       printf( "Origin: %20s\n",msg->mborig  );
  233.       printf( "Room:   %20s  ",msg->mbroom  );
  234.       printf( "Src Id: %20s\n",msg->mbsrcId );
  235.       printf( "Net Adr:%s\n",msg->mbaddr  );
  236.       printf( "Other:%s\n",msg->mbOther );
  237.       printf( "Reply:  %20s\n",msg->mbreply );
  238.       printf( "Domain:%s\n",msg->mbdomain);
  239.       };
  240.     Do_Statistics(msg);
  241.     };
  242.   }
  243.  
  244. BOOLEAN_TYPE match(char *str1, int idx)
  245.   {
  246.   char *c1;
  247.   char *c2;
  248.   int  siz;
  249.   int  dif;
  250.   siz = options[idx].size;
  251.   c1  = str1;
  252.   c2  = option_data[idx];
  253.   dif = 0;
  254.   while ( dif == 0 && *c1 != '\0' && *c1 != '\0' && siz >0 )
  255.     {
  256.     siz--;
  257.     if( *c2 == '*')break ;
  258.     if( *c2 != '?')dif = *c1 - *c2;
  259.     c1++;
  260.     c2++;
  261.     };
  262.   return ( dif == 0 ) ? YES : NO;
  263.   }
  264.  
  265. struct headlist
  266.   {
  267.   struct headlist *next; /* next item in the list */
  268.   char *name;            /* name of primary item */
  269.   long  count;           /* occurances */
  270.   };
  271.  
  272. struct headlist *msg_per_room= NULL;
  273. struct headlist *msg_per_node= NULL;
  274. struct headlist *msg_per_domain= NULL;
  275. struct headlist *msg_per_author=NULL;
  276. long msg_per_hour[24] = { 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0 };
  277.  
  278. void Do_Statistics(MessageBuffer *msg)
  279.   {
  280.   char *tptr;
  281.   int hour;
  282.   Increment(&msg_per_room, msg->mbroom);
  283.   Increment(&msg_per_node, msg->mborig);
  284.   Increment(&msg_per_author, msg->mbauth);
  285.   Increment(&msg_per_domain, msg->mbdomain);
  286.   tptr = &msg->mbtime[0];
  287.   while( *tptr == ' ')tptr++;
  288.   if( *tptr >= '0' && *tptr <= '9' )
  289.     {
  290.     hour = tptr[0] - '0';
  291.     tptr++;
  292.     if( *tptr >= '0' && *tptr <= '9')
  293.       {
  294.       hour =  ( hour * 10 );
  295.       hour += ( *tptr - '0');
  296.       };
  297.     if( hour == 12 ) hour=0;
  298.     if( tptr[4] == 'p')hour +=12;
  299.     if( hour < 0 || hour > 23 )
  300.       {
  301.       printf("computed %d for %s, invalid time\n", hour, msg->mbtime);
  302.       }
  303.     else msg_per_hour[hour]++;
  304.     };
  305.   }
  306.  
  307. void Increment( struct headlist **cptr, char *name)
  308.   {
  309.   struct headlist *pptr;
  310.   pptr = *cptr;
  311.   if( *name == '\0' || *name == ' ' )
  312.     {
  313.     name = "*** local ***";
  314.     };
  315.   while ( pptr != NULL )
  316.     {
  317.     if( strcmp(name, pptr->name) == 0 )
  318.       {
  319.       pptr->count++;
  320.       return;
  321.       };
  322.     pptr = pptr->next;
  323.     };
  324.   pptr = (struct headlist *)malloc(sizeof(struct headlist));
  325.   if( pptr == NULL )
  326.     {
  327.     printf("Error: No memory available\n");
  328.     exit(100);
  329.     };
  330.   pptr->name = strdup(name);
  331.   pptr->count= 1;
  332.   pptr->next = *cptr;
  333.   *cptr = pptr;
  334.   }
  335.  
  336. void Print_Items(struct headlist *ptr)
  337.   {
  338.   int count=0;
  339.   while( ptr )
  340.     {
  341.     printf("%20s  %10ld    ", ptr->name, ptr->count);
  342.     count = ( count + 1 ) % 2;
  343.     if( count == 0 ) printf("\n");
  344.     ptr= ptr->next;
  345.     };
  346.   }
  347.  
  348. void Print_Statistics(void)
  349.   {
  350.   char *h1 = "Room Name";
  351.   char *h2 = "Count";
  352.   char *h3 = "Node";
  353.   char *h4 = "Author";
  354.   char *h5 = "Domain";
  355.   char *h6 = "Hour  ";
  356.   char *h7 =  "am";
  357.   char *h8 =  "pm";
  358.   int hour;
  359.   printf("\n\n            Messages Per Room  Report\n");
  360.   printf("%20s  %10s    %20s  %10s\n", h1, h2, h1, h2);
  361.   Print_Items( msg_per_room );
  362.   printf("\n\n            Messages Per Node  Report\n");
  363.   printf("%20s  %10s    %20s  %10s\n", h3, h2, h3);
  364.   Print_Items( msg_per_node );
  365.   printf("\n\n            Messages Per Author  Report\n");
  366.   printf("%20s  %10s    %20s  %10s\n", h4, h2, h4);
  367.   Print_Items( msg_per_author );
  368.   printf("\n\n            Messages Per Domain  Report\n");
  369.   printf("%20s  %10s    %20s  %10s\n", h5, h2, h5);
  370.   Print_Items( msg_per_domain );
  371.   printf("\n\n            Messages Per Hour  Report\n");
  372.   printf("%20s  %10s    %20s  %10s\n", h6, h2, h6);
  373.   for(hour=0; hour < 12; hour +=2 )
  374.     {
  375.     printf("%17d %s  %10ld",( hour == 0 ) ? 12 : hour, h7, msg_per_hour[hour]);
  376.     printf("    %17d %s  %10ld\n",hour+1, h7, msg_per_hour[hour+1]);
  377.     };
  378.   for(hour=12; hour < 24; hour +=2 )
  379.     {
  380.     printf("%17d %s  %10ld",(hour == 12) ? 12: hour-12, h8, msg_per_hour[hour]);
  381.     printf("    %17d %s  %10ld\n", hour-11, h8, msg_per_hour[hour+1]);
  382.     };
  383.   }
  384.